{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FFI 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "from tvm import ffi as tvm_ffi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "fecho = tvm_ffi.get_global_func(\"testing.echo\")\n",
    "s = tvm_ffi.String(\"hello\")\n",
    "assert isinstance(s, tvm_ffi.String)\n",
    "s2 = fecho(s)\n",
    "assert s2.__tvm_ffi_object__.same_as(s.__tvm_ffi_object__)\n",
    "\n",
    "s3 = tvm_ffi.convert(\"hello\")\n",
    "assert isinstance(s3, tvm_ffi.String)\n",
    "assert isinstance(s3, str)\n",
    "\n",
    "s4 = pickle.loads(pickle.dumps(s))\n",
    "assert s4 == \"hello\"\n",
    "assert isinstance(s4, tvm_ffi.String)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bytes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "fecho = tvm_ffi.get_global_func(\"testing.echo\")\n",
    "b = tvm_ffi.Bytes(b\"hello\")\n",
    "assert isinstance(b, tvm_ffi.Bytes)\n",
    "b2 = fecho(b)\n",
    "assert b2.__tvm_ffi_object__.same_as(b.__tvm_ffi_object__)\n",
    "\n",
    "b3 = tvm_ffi.convert(b\"hello\")\n",
    "assert isinstance(b3, tvm_ffi.Bytes)\n",
    "assert isinstance(b3, bytes)\n",
    "\n",
    "b4 = tvm_ffi.convert(bytearray(b\"hello\"))\n",
    "assert isinstance(b4, tvm_ffi.Bytes)\n",
    "assert isinstance(b4, bytes)\n",
    "\n",
    "b5 = pickle.loads(pickle.dumps(b))\n",
    "assert b5 == b\"hello\"\n",
    "assert isinstance(b5, tvm_ffi.Bytes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
